/* eslint-disable camelcase */
import queryString from 'query-string';
import openApiRequest from '../utils/openApiRequest';

export async function getShareUsers(payload: any) {
  console.log('service/openapi/getShareUsers');
  return openApiRequest(
    `app/sharing${payload ? `?${queryString.stringify(payload)}` : ''}`,
    { method: 'GET' }
  );
}

export async function shareDevice({ body }: { body: any }) {
  console.log('server/openapi/shareDevice', body, JSON.stringify(body));
  return openApiRequest('app/sharing', {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

export async function bindQrcodeDevice(content: string) {
  return openApiRequest('app/bind_latest', {
    method: 'POST',
    body: JSON.stringify({
      qr_content: content,
      set_owner: 0,
    }),
  });
}

export async function queryDataPoint({ productKey }: { productKey: string }) {
  return openApiRequest(`app/datapoint?product_key=${productKey}`, {
    method: 'GET',
  });
}

export async function replyShareOffer({
  shareId,
  status,
}: {
  shareId: number;
  status: string;
}) {
  console.log('server/openapi/replyShareOffer', shareId, status);
  return openApiRequest(`app/sharing/${shareId}?status=${status}`, {
    method: 'PUT',
  });
}

export async function cancelShareDevice({ shareId }: { shareId: number }) {
  console.log('server/openapi/cancelShareDevice', shareId);
  return openApiRequest(`app/sharing/${shareId}`, { method: 'DELETE' });
}

export async function setUserAlias({
  shareId,
  userAlias,
}: {
  shareId: number;
  userAlias: string;
}) {
  console.log('server/openapi/setUserAlias', shareId, userAlias);
  return openApiRequest(
    `app/sharing/${shareId}/alias?user_alias=${userAlias}`,
    { method: 'PUT' }
  );
}

export async function forgotPassword(payload: any) {
  console.log('/app/reset_password', payload, JSON.stringify(payload));
  return openApiRequest(
    'app/reset_password',
    {
      method: 'POST',
      body: JSON.stringify(payload),
    },
    false
  );
}

export async function getGroups() {
  console.log('/app/group');
  return openApiRequest('app/group', { method: 'GET' });
}

export async function createGroup({
  productKey,
  groupName,
}: {
  productKey: string;
  groupName: string;
}) {
  console.log('post /app/group', productKey, groupName);
  return openApiRequest('app/group', {
    method: 'POST',
    body: JSON.stringify({
      product_key: productKey,
      group_name: groupName,
    }),
  });
}

export async function bindDeviceByQRCode({ qrContent }: { qrContent: string }) {
  console.log('post bindDeviceByQRCode:/app/bind_latest', qrContent);
  return openApiRequest('app/bind_latest', {
    method: 'POST',
    body: JSON.stringify({
      qr_content: qrContent,
    }),
  });
}

export async function sharingCode({ code }: { code: string }) {
  console.log('post /sharing/code', code);
  return openApiRequest(`app/sharing/code/${code}`, {
    method: 'POST',
  });
}

export async function controlDevice({ did, data }: { did: string; data: any }) {
  console.log('=========', did, data);
  return openApiRequest(`app/control/${did}`, {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

export function textAnalyze(data: any) {
  const path = 'voice/v1/text_analyze';
  console.log(`post ${path}`, data);
  return openApiRequest(path, {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

export async function getLicense({
  type,
  language,
}: {
  type: string;
  language: string;
}) {
  const path = `app/terms/release/url?locate=${language}&terms_type=${type}`;
  console.log(`post ${path}`);
  return openApiRequest(
    path,
    {
      method: 'GET',
    },
    false
  );
}

export async function checkNewLicense({ language }: { language: string }) {
  const path = `app/users/terms?locate=${language}`;
  console.log(`post ${path}`);
  return openApiRequest(path, {
    method: 'GET',
  });
}

export async function confirmLicense(
  {
    terms_type,
    version,
    language,
  }: { terms_type: string; version: string; language: string }
) {
  const path = 'app/users/terms';
  console.log(`post ${path}`);
  return openApiRequest(path, {
    method: 'PUT',
    body: JSON.stringify({
      locate: language,
      terms: [
        {
          terms_type,
          version,
        },
      ],
    }),
  });
}

// export async function destructionAccount() {
//   const openApiUrl = await ServiceInfoManager.getOpenApiUrl();
//   const url = `${openApiUrl}app/users`;
//   const options = {
//     method: 'DELETE',
//   };
//   let appID = gizwitsConfig.androidAppID;

//   try {
//     if (Platform.OS === 'android') {
//       // if (false) {
//       appID = gizwitsConfig.androidAppID;
//     } else {
//       appID = gizwitsConfig.iosAppID;
//     }
//   } catch (error) {
//   }
//   options.headers = {
//     'X-Gizwits-Application-Id': appID,
//     'X-Gizwits-User-token': global.token,
//   };

//   return gizwitsRequest(url, options);
// }

// export function updateUserInfo(data) {
//   const path = 'app/users';
//   console.log(`post ${path}`, data);
//   return openApiRequest(path, {
//     method: 'PUT',
//     body: JSON.stringify(data),
//   });
// }

export async function deviceLatestData(did: string) {
  const url = `app/devdata/${did}/latest?show_expected_status=1`;
  const options = {
    method: 'GET',
  };
  return openApiRequest(url, options);
}

export async function fetchSubDevices(did: string, skip: number = 0, limit: number = 20) {
  const url = `app/devices/${did}/slave?skip=${skip}&limit=${limit}`;
  const options = {
    method: 'GET',
  };
  return openApiRequest(url, options);
}

export async function fetchDeviceList(skip: number = 0, limit: number = 200) {
  const url = `app/bindings?limit=${limit}&skip=${skip}`;
  const options = {
    method: 'GET',
  };
  return openApiRequest(url, options);
}

export async function getDeviceTimeZone({ did, productKey }: { did: string, productKey: string }) {
  const url = `v2/provisions/products/${productKey}/devices/${did}`;
  const options = {
    method: 'GET',
  };
  return openApiRequest(url, options);
}

export async function setDeviceTimeZone(
  { did, productKey, timezone_id }:
    { did: string, productKey: string, timezone_id: string }
) {
  const url = `v2/provisions/products/${productKey}/devices/${did}`;
  // eslint-disable-next-line no-undef
  const formData = new FormData();
  formData.append('timezone_id', timezone_id);
  const options = {
    method: 'PUT',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    body: formData,
  };
  return openApiRequest(url, options);
}

export async function getDeviceHardInfo({ did }: { did: string }) {
  const url = `api/otav41_information/${did}`;
  const options = {
    method: 'GET',
  };
  return openApiRequest(url, options);
}
